﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QMSDAL;

namespace QuaManSys.Rights
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //判断用户是否是非法访问
            if (Session["CurUser"] != null)
            {
                //从session中取出当前用户信息并显示
                CurUser user = (CurUser)Session["CurUser"];
                BindModules();
            }
            else
            {
                //跳转到登录页面
                Response.Redirect("~/Login.aspx");
            }
        }

        /// <summary>
        /// 获取模块权限列表
        /// </summary>
        protected void BindModules()
        {
            using (QuaManSysEntities entity = new QuaManSysEntities())
            {
                var queryMenu = from q in entity.SysMenu
                                orderby q.Id
                                select q;

                ModuleView.DataSource = queryMenu.ToList();
                ModuleView.DataBind();

            }
        }

        protected void ModuleView_RowDataBound(object sender, GridViewRowEventArgs equals)
        {
            CurUser user = (CurUser)Session["CurUser"];
            string strId = user.employeeId;
            string dept = user.department;
            if (equals.Row.RowType == DataControlRowType.DataRow)
            {

                CheckBoxList AuthorityList_Grant = (CheckBoxList)equals.Row.FindControl("AuthorityList_Grant");
                Label lab_ID = (Label)equals.Row.FindControl("lab_ID");
                Label lab_Verify = (Label)equals.Row.FindControl("lab_Verify");

                using (QuaManSysEntities entity = new QuaManSysEntities())
                {
                    var queryAllOper = from p in entity.SysOperation
                                       join q in entity.SysMenuSysOperation
                                       on p.Id equals q.SysOperationId
                                       where q.SysMenuId == lab_ID.Text
                                       orderby q.SysOperationId
                                       select p;

                    var queryPersonOper = (from q in entity.SysOperation
                                           join p in entity.SysMenuSysRoleSysOperation
                                           on q.Id equals p.SysOperationId
                                           join r in entity.SysRoleSysPerson
                                           on p.SysRoleId equals r.SysRoleId
                                           where r.SysPersonId == strId 
                                           select q).Distinct();
                    var queryDeptOper = (from p in entity.SysOperation
                                         join q in entity.SysMenuSysRoleSysOperation
                                             on p.Id equals q.SysOperationId
                                         join r in entity.SysRoleSysDept
                                             on q.SysRoleId equals r.SysRoleId
                                         where r.Dept == dept
                                         select p).Distinct();

                    int n = queryAllOper.Count();
                    if (n != 0)
                    {
                        bool[] vstate = new bool[n];
                        bool operationSelected = false;
                        int i = 0;
                        foreach (var item in queryAllOper)
                        {
                            foreach (var it in queryPersonOper)
                            {
                                if (it == item)
                                {
                                    operationSelected = true;
                                    break;
                                }
                            }
                            if (!operationSelected)
                            {
                                foreach (var it in queryDeptOper)
                                {
                                    if (it == item)
                                    {
                                        operationSelected = true;
                                        break;
                                    }
                                }
                            }
                            AuthorityList_Grant.Items.Add(new ListItem(item.Name, item.Id, false));
                            vstate[i] = operationSelected;//初始状态数组;
                            i++;
                            operationSelected = false;
                        }
                        for (i = 0; i < n; i++)
                        {
                            AuthorityList_Grant.Items[i].Selected = vstate[i];
                        }
                        AuthorityList_Grant.DataBind();
                    }
                }
            }
        }

    }
}